﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using eBay.Service.Core.Sdk;
using Samples.Helper;

using eBay.Service.Core.Soap;
using eBay.Service.Call;
using eBay.Service.Util;

namespace EbayMaster
{
    public partial class    FrmAskBuyer : Form
    {
        public EbayTransactionType EbayTransaction = null;
        public AccountType Account = null;
        public bool SentMessage = false;

        public FrmAskBuyer()
        {
            InitializeComponent();
        }

        private void LoadSellerMessageTemplates()
        {
            string[] messageTemplates = EbayConstants.sellerMessageTemplates;
            foreach (string msgTemplate in messageTemplates)
            {
                this.listBoxMessageTemplate.Items.Add(msgTemplate);
            }
        }

        private void FrmAskBuyer_Load(object sender, EventArgs e)
        {
            if (EbayTransaction == null || Account == null)
            {
                MessageBox.Show("无效数据", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            this.textBoxBuyerId.Text = EbayTransaction.BuyerId;
            this.textBoxSellerName.Text = EbayTransaction.SellerName;
            this.textBoxItemTitle.Text = EbayTransaction.ItemTitle;
            LoadSellerMessageTemplates();
        }

        private void listBoxMessageTemplate_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] messageTemplates = EbayConstants.sellerMessageTemplates;
            int selectedIndex = this.listBoxMessageTemplate.SelectedIndex;
            if (selectedIndex >= 0 && selectedIndex < EbayConstants.sellerMessageTemplatesCount)
            {
                this.textBoxMessage.Text = messageTemplates[selectedIndex];
            }
        }

        private void buttonSendMessage_Click(object sender, EventArgs e)
        {
            // sanity check.
            if (this.textBoxMessage.Text == null || this.textBoxMessage.Text.Trim().Length == 0)
            {
                MessageBox.Show("无效的消息正文", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (EbayTransaction == null || Account == null)
                return;

            String subject = String.Format("{0} sent a message on item {1} with itemId {2}",
                EbayTransaction.SellerName, 
                EbayTransaction.ItemTitle, 
                EbayTransaction.ItemId);

            this.buttonSendMessage.Enabled = false;
            this.buttonSendMessage.Text = "正在发送消息......";
                
            bool result = EbayMessageBiz.SendMessageToBuyer(Account, 
                EbayTransaction.BuyerId, 
                EbayTransaction.ItemId,
                subject, 
                this.textBoxMessage.Text, 
                true/*emailCopyToSender*/, 
                eBay.Service.Core.Soap.QuestionTypeCodeType.General);

            if (result)
            {
                DateTime startTime = DateTime.Now;
                DateTime endTime = startTime;
                startTime = startTime.Subtract(new TimeSpan(0, 5, 0));

                startTime = startTime.ToUniversalTime();
                endTime = endTime.ToUniversalTime();

                StringCollection msgIds = EbayMessageBiz.GetAllMessageIds(Account, startTime, endTime);
                EbayMessageBiz.GetAllMessageByIds(Account, msgIds);

                SentMessage = true;

                MessageBox.Show("成功发送消息\r\n", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("发送消息错误，请查看日志", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.buttonSendMessage.Enabled = true;
            this.buttonSendMessage.Text = "发送";

            this.Close();
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape)
                this.Close();
            return base.ProcessCmdKey(ref msg, keyData);
        } 
    }
}
